All files / web/src/app/api/curriculum/[playerId]/session-mode route.ts

0% Statements 0/58
0% Branches 0/1
0% Functions 0/1
0% Lines 0/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                                                                                                                     
/**
 * API route for getting the session mode for a student
 *
 * GET /api/curriculum/[playerId]/session-mode
 *
 * Returns the unified session mode that determines:
 * - What type of session should be run (remediation/progression/maintenance)
 * - What to show in the dashboard banner
 * - What CTA to show in the StartPracticeModal
 * - What problems the session planner should generate
 *
 * This is the single source of truth for session planning decisions.
 */

import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { canPerformAction } from '@/lib/classroom'
import { getSessionMode, type SessionMode } from '@/lib/curriculum/session-mode'
import { getSessionModeComfortLevel } from '@/lib/curriculum/session-mode-comfort'
import { getUserId } from '@/lib/viewer'

export interface SessionModeResponse {
  sessionMode: SessionMode
  comfortLevel: number
  comfortByMode: Record<string, number>
}

/**
 * GET - Get the session mode for a student
 */
export const GET = withAuth(async (_request, { params }) => {
  try {
    const { playerId } = (await params) as { playerId: string }

    if (!playerId) {
      return NextResponse.json({ error: 'Player ID required' }, { status: 400 })
    }

    // Authorization check
    const userId = await getUserId()
    const canView = await canPerformAction(userId, playerId, 'view')
    if (!canView) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const sessionMode = await getSessionMode(playerId)
    const comfortResult = await getSessionModeComfortLevel(playerId, sessionMode)

    return NextResponse.json({
      sessionMode,
      comfortLevel: comfortResult.overall,
      comfortByMode: comfortResult.byMode,
    } satisfies SessionModeResponse)
  } catch (error) {
    console.error('Error fetching session mode:', error)
    return NextResponse.json({ error: 'Failed to fetch session mode' }, { status: 500 })
  }
})